#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "datamanager.h"

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);

    //1. 注册dataManager对象给qml, qml中通过第一参数使用的名字使用
    //2. 注册的对象需要继承自QObject
    DataManager dataManager;
    engine.rootContext()->setContextProperty("dataManager", &dataManager);
    engine.load(url);

    //engine.rootObjects().first() 查找的是main.qml中的第一个根对象, 如果需要绑定其他对象可以通过findChild实现
    QList<QObject*> list = engine.rootObjects();
    if(list.isEmpty()) {
        QCoreApplication::exit(-1);
    }
    QObject *qmlObj = list.first();
    if(!qmlObj) {
        QCoreApplication::exit(-1);
    }
    QObject::connect(qmlObj, SIGNAL(changeDefault(QString)), &dataManager, SLOT(receiveQmlSignal(QString)));

    return app.exec();
}
